package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.http.HttpRequest;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissonService;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Jerry du
 * @version 1.0
 * @date 2019/12/31 9:48
 */
@RestController
@RequestMapping("/permission")
public class PermissonController {

    @Reference
    private PermissonService permissonService;

    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        try {
            //String createdate = request.getParameter("createdate");
            //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String date=df.format(new Date()).toString();// new Date()为获取当前系统时间
            permission.setCreateDate(date);

           // permission.setCreateDate(date);
            //获取当前
            System.out.println(permission);
            permissonService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSON_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSON_FAIL);
        }

    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        return permissonService.findPage(pageBean);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Permission permission = permissonService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try{
            permissonService.edit(permission);//发送请求
            return new Result(true, MessageConstant.EDIT_PERMISSON_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSON_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result del(Integer id){
        try {
            permissonService.del(id);
            return new Result(true,MessageConstant.DEL_PERMISSON_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DEL_PERMISSON_FAIL);
        }
    }
    @Test
    public void test(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        System.out.println(df.format(new Date()).toString());
    }
}
